#ifdef KINECT

#include "inc\GUI\kinect\kinectkeyframedialog.h"
#include "ui_kinectkeyframedialog.h"

#include "Kinect/kinectimage.h"

KinectKeyFrameDialog::KinectKeyFrameDialog(kinect::Kinect* kinect, QWidget *parent) :
    KinectDialog(kinect, parent),
    ui(new Ui::KinectKeyFrameDialog),
    _pose(NULL)
{
    ui->setupUi(this);

    ui->troyLive->resize(ui->troyLive->size());
    ui->troyLive->mirror(true);
    ui->troySnapshot->resize(ui->troySnapshot->size());
    ui->troySnapshot->mirror(true);

    connect(ui->takeSnapshot, SIGNAL(clicked()), this, SLOT(takeSnapshot()));
    connect(ui->buttonBox->button(QDialogButtonBox::Ok), SIGNAL(clicked()), this, SLOT(accept()));
    connect(ui->buttonBox->button(QDialogButtonBox::Cancel), SIGNAL(clicked()), this, SLOT(reject()));

    kinect::KinectImage* image = getKinect()->addComponent<kinect::KinectImage>();
    connect(image, SIGNAL(depthMapChanged(QImage*)), this, SLOT(updateImage(QImage*)));

    maximizeSize();
}

KinectKeyFrameDialog::~KinectKeyFrameDialog() {
    delete ui;
    // The KinectSkeleton isn't removed from the _kinect object because it will be needed again, and this way it might not
    // need to be recalibrated.
    //getKinectSkeleton()->requireTracking(false);
}

#ifdef WIIMOTE
void KinectKeyFrameDialog::associateWiimote(WiimoteStatusDialog *dialog) {
    if (hasWiimoteDialog()) {
        getWiimoteDialog()->setCommand(qWiimote::A, "Take Snapshot", this, SLOT(takeSnapshot()));
        getWiimoteDialog()->setCommand(qWiimote::Plus, "OK", this, SLOT(accept()));
        getWiimoteDialog()->setCommand(qWiimote::Minus, "Cancel", this, SLOT(reject()));
        getWiimoteDialog()->setCommand(qWiimote::B, "Cancel", this, SLOT(reject()));
        getWiimoteDialog()->setCommand(qWiimote::Home, "Reset", getKinectSkeleton(), SLOT(reset()));
    }
}
#endif

/*
MotionKeyFrame KinectKeyFrameDialog::getKeyFrame() {
    MotionKeyFrame keyFrame;
    if (result() == QDialog::Accepted) {
        keyFrame = ui->troySnapshot->getKeyFrame();
    }
    return keyFrame;
}
*/

QSharedPointer<TroyPose> KinectKeyFrameDialog::getRecordedPose() const {
    return _pose;
}

void KinectKeyFrameDialog::takeSnapshot() {
    ui->troySnapshot->loadKeyFrame(ui->troyLive->getKeyFrame());
}

void KinectKeyFrameDialog::updateImage(QImage* image) {
    ui->kinectCamera->setPixmap(QPixmap::fromImage(*image));
}

/*
void KinectKeyFrameDialog::kinectKeyFrame(MotionKeyFrame keyFrame) {
    ui->troyLive->loadKeyFrame(keyFrame);
}
*/
void KinectKeyFrameDialog::receiveTroyPose(QSharedPointer<TroyPose> pose) {
    _pose = pose;
}

#endif
